##################################################
## Replication materials for 
## "Competent legislators or mere pawns? 
## Experimental evidence of attitudes toward gender quota politicians"
## by Carolyn Barnett, Alexandra Blackman, and Marwa Shalaby

## 7. Appendix: Secondary Hypotheses 
## This file creates Appendix Figures A.12 to A.14 and Tables A.13 to A.15

## NOTE: Run file "01_setup.R" first to load necessary packages and data

# Additional Set-Up ---------------------------

# Transform dataset to long format 
morvign_long <- gather(morvign, measure, value, 
                       c(vignette_competence:vignette_pawnlike), 
                       factor_key=TRUE)

# Recode variables
morvign_long$vignette_election <- factor(morvign_long$vignette_election)
morvign_long$vignette_mode <- factor(morvign_long$vignette_mode)
morvign_long$vignette_work <- factor(morvign_long$vignette_work)
morvign_long$measure <- factor(morvign_long$measure, levels= c("vignette_competence", 
                                                               "vignette_pawnlike"))


# Create just female politicians dataset
morvign_fempol <- morvign %>% filter(vignette_gender == "Woman")

# Create just quota women politicians dataset
morvign_wq <- morvign %>% filter(vignette_election=="Woman/quota")

# RESPONDENT GENDER --------------

## FIGURE A.12 -----------

vign_election_respgen <- summarySE(morvign_long, measurevar="value", 
                                   groupvars=c("measure","vignette_election","RespGender"), na.rm= T)

vign_election_respgen <- vign_election_respgen %>% 
  filter(!is.na(vignette_election)) 

vign_election_respgen

ggplot(vign_election_respgen, aes(x=RespGender, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Respondent Gender", 
       x = "Respondent Gender", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_respgen.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)


###################################################
#       Difference in Differences                 #
#       vignette_mode * RespGender                #
###################################################

m_respgen_com1 <- lm(vignette_competence ~ vignette_mode * RespGender, data=morvign)
summary(m_respgen_com1)

m_respgen_pawn1 <- lm(vignette_pawnlike ~ vignette_mode * RespGender, data=morvign)
summary(m_respgen_pawn1)

## TABLE A.13 ------------

# create tidy table with t-tests for variables of interest
morvign_fempol_respgen_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ RespGender, morvign_fempol)),
                                        tidy(t.test(vignette_pawnlike ~ RespGender, morvign_fempol))
)

# prepare table for latex estimate1 = male resp & estimate2 = female resp
morvign_fempol_respgen_t_tests <- morvign_fempol_respgen_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (Male Respondents)" = estimate1,
                "Mean (Female Respondents)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

stargazer(morvign_fempol_respgen_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)


# HOSTILE SEXISM ------------------

## FIGURE A.13 -----------

vign_election_hostile <- summarySE(morvign_long, measurevar="value", 
                                   groupvars=c("measure","vignette_election","Hostile"), na.rm= T)

vign_election_hostile <- vign_election_hostile %>% 
  filter(!is.na(Hostile))  

vign_election_hostile

##### CREATE GRAPH

ggplot(vign_election_hostile, aes(x=Hostile, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Hostile Sexism", 
       x = "Hostile Sexism", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_hostile.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)

###################################################
#       Difference in Differences                 #
#       vignette_mode * Hostile                   #
###################################################

m_hostile_com1 <- lm(vignette_competence ~ vignette_mode * Hostile, data=morvign)
summary(m_hostile_com1)

m_hostile_pawn1 <- lm(vignette_pawnlike ~ vignette_mode * Hostile, data=morvign)
summary(m_hostile_pawn1)


## TABLE A.14 ------------

# create tidy table with t-tests for variables of interest
morvign_fempol_hostile_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ Hostile, morvign_fempol)),
                                        tidy(t.test(vignette_pawnlike ~ Hostile, morvign_fempol))
)

# prepare table for latex estimate1 = low hostile sexism & estimate2 = high hostile sexism
morvign_fempol_hostile_t_tests <- morvign_fempol_hostile_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (Low Hostile Sexism)" = estimate1,
                "Mean (High Hostile Sexism)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

stargazer(morvign_fempol_hostile_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)


# PATRIARCHAL ATTITUDES ------------

## FIGURE A.14 -----------

vign_election_patriarchal <- summarySE(morvign_long, measurevar="value", 
                                       groupvars=c("measure","vignette_election","Patriarchal"), na.rm= T)

vign_election_patriarchal <- vign_election_patriarchal %>% 
  filter(!is.na(Patriarchal)) 

vign_election_patriarchal

##### CREATE GRAPH

ggplot(vign_election_patriarchal, aes(x=Patriarchal, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Patriarchal Norms", 
       x = "Patriarchal Norms", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_patriarchal.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)


###################################################
#       Difference in Differences                 #
#       vignette_mode * Patriarchal               #
###################################################

m_patr_com1 <- lm(vignette_competence ~ vignette_mode * Patriarchal, data=morvign)
summary(m_patr_com1)

m_patr_pawn1 <- lm(vignette_pawnlike ~ vignette_mode * Patriarchal, data=morvign)
summary(m_patr_pawn1)

## TABLE A.15 ------------

# create tidy table with t-tests for variables of interest
morvign_fempol_patr_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ Patriarchal, morvign_fempol)),
                                     tidy(t.test(vignette_pawnlike ~ Patriarchal, morvign_fempol))
)

# prepare table for latex estimate1 = low patriarchal attitudes & estimate2 = high patriarchal attitudes
morvign_fempol_patr_t_tests <- morvign_fempol_patr_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (Low Patriarchal Attitudes)" = estimate1,
                "Mean (High Patriarchal Attitudes)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

stargazer(morvign_fempol_patr_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)

